package com.marisfrolg.ui;

import com.marisfrolg.storage.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class ProgressBarWithText extends ProgressBar {
	String text;
	Paint mPaint;
	int pColor=Color.argb(127, 219, 183, 107);//finished 
	int fColor=Color.argb(127, 28, 156, 116);

	public ProgressBarWithText (Context context) {
		super(context);
		initText();
	}

	public ProgressBarWithText (Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initText();
	}

	public ProgressBarWithText (Context context, AttributeSet attrs) {
		super(context, attrs);
		initText();
	}

	@Override
	public synchronized void setProgress(int progress) {
		setText(progress);
		if(progress==getMax()){
			this.setProgressDrawable(getContext().getResources().getDrawable(R.drawable.progressbarfstyle));
		}else{
			this.setProgressDrawable(getContext().getResources().getDrawable(R.drawable.progressbarstyle));
		}
		super.setProgress(progress);
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected synchronized void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//this.setText();
		Rect rect = new Rect();
		this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
		int x = (getWidth() / 2) - rect.centerX();

		int y = (getHeight() / 2) - rect.centerY();
		mPaint.setTextSize(25);
		canvas.drawText(this.text, x, y, this.mPaint);
	}

//初始化，画笔
	private void initText() {
		this.mPaint = new Paint();
		this.mPaint.setColor(Color.BLACK);
	}

//	private void setText() {
//		setText(this.getProgress());
//	}

//设置文字内容
	private void setText(int progress) {
		this.text = "完成进度:"+progress+"/"+this.getMax();
	}
}
